<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Response;
use App\Models\Admin\ArticleClass;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class ArticleClassController extends Controller
{
    /**
     * @var UserRepository
     */
    private $apage;


    /**
     * ArticleClassApageController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->articleClass = new ArticleClass();
    }

    /**
     * 单页列表
     *
     * @return Response
     */
    public function index()
    {
        $apageObj = $this->articleClass->orderBy('class_id', 'desc')->paginate(15, ['*'], 'pageNum');
        return view('admin.ArticleClass.index', ['apage' => $apageObj]);
    }

    /**
     * 创建单页
     *
     * @return Response
     */
    public function create(Request $request)
    {
        if ($request->getMethod() == "POST") {

            $validator = Validator::make($request->all(),
                [
                    'class_name' => 'required|min:2|max:255',
                ],
                [
                    'class_name.required' => '分类名称不能为空',
                    'class_name.min' => '分类名称内容不能小于2个汉字',
                    'class_name.max' => '分类名称内容不能大于100个汉字',
                ]
            );
            if ($validator->fails()) {
                return view('admin.ArticleClass.create')->withErrors($validator);
            }
            $insert = $this->articleClass;
            $insert->class_name = $request->input('class_name');
            $insert->class_keyword = $request->input('class_keyword');
            $insert->class_sort = $request->input('class_sort');
            if ($insert->save()) {
                return redirect()->route('admin.ArticleClass')->with('success','创建成功!');
            } else {
                return view('admin.ArticleClass.create')->withErrors($insert->save());
            }
        }
        return view('admin.ArticleClass.create');


    }


    /**
     * 编辑单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit(Request $request,$id)
    {
        $apageObj=$this->articleClass->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.ArticleClass')->withErrors(compact('errors'));
        }
        if($request->getMethod()=="POST"){
            $apageObj->class_name = $request->input('class_name');
            $apageObj->class_keyword = $request->input('class_keyword');
            $apageObj->class_sort = $request->input('class_sort');
            $apageObj->update();
            return redirect()->route('admin.ArticleClass')->with('success','保存成功!');

        }
        return view('admin.ArticleClass.edit', ['apage'=>$apageObj]);
    }


    /**
     * 删除单页
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $apageObj=$this->articleClass->find($id);
        if (!$apageObj) {
            $errors='内容不存在！';
            return redirect()->route('admin.ArticleClass')->withErrors(compact('errors'));
        }
        $apageObj->delete($id);

        return redirect()->route('admin.ArticleClass')->with('success','删除成功!');
    }


}
